/*
* Copyright 2024 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "events_init.h"
#include <stdio.h>
#include "lvgl.h"
#include "esp_log.h"

#include "esp_system.h"
#include "wifi_sta.h"
#include "custom.h"

#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif

const static char *TAG = "UIEVT";

static void main_imgbtn_settings_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.settings, guider_ui.settings_del, &guider_ui.main_del, setup_scr_settings, LV_SCR_LOAD_ANIM_FADE_ON, 200, 200, false, true);
        break;
    }
    default:
        break;
    }
}

static void main_sw_led_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_VALUE_CHANGED:
    {
        lv_obj_t * status_obj = lv_event_get_target(e);
        int status = lv_obj_has_state(status_obj, LV_STATE_CHECKED) ? 1 : 0;
        custom_set_main_led(&guider_ui, status);
        custom_set_led_status(custom_get_select_device_list(&guider_ui), status);
        break;
    }
    default:
        break;
    }
}

static void main_label_time_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch(code)
    {
        case LV_EVENT_DELETE:
        {
            lv_ui *ui = lv_event_get_user_data(e);
            ui->main_label_time = NULL;
        }
        break;
        default:
        break;
    }
}

void events_init_main (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->main_imgbtn_settings, main_imgbtn_settings_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_sw_led, main_sw_led_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->main_label_time, main_label_time_event_handler, LV_EVENT_ALL, ui);
}

static void settings_list_settings_item0_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, &guider_ui.settings_del, setup_scr_main, LV_SCR_LOAD_ANIM_FADE_ON, 200, 200, false, true);
        break;
    }
    default:
        break;
    }
}

static void settings_list_settings_item1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.wifi, guider_ui.wifi_del, &guider_ui.settings_del, setup_scr_wifi, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 200, 200, false, true);
        wifi_sta_scan_aps_start();
        break;
    }
    default:
        break;
    }
}

static void settings_list_settings_item2_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.devices, guider_ui.devices_del, &guider_ui.settings_del, setup_scr_devices, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 200, 200, false, true);
        break;
    }
    default:
        break;
    }
}

void events_init_settings (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->settings_list_settings_item0, settings_list_settings_item0_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->settings_list_settings_item1, settings_list_settings_item1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->settings_list_settings_item2, settings_list_settings_item2_event_handler, LV_EVENT_ALL, ui);
    ESP_LOGI(TAG, "%s:%d", __func__, __LINE__);
}

static void wifi_list_wifis_item0_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_ui *ui = (lv_ui *)lv_event_get_user_data(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(ui, &ui->settings, ui->settings_del, &ui->wifi_del, setup_scr_settings, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 200, false, true);
        wifi_sta_scan_aps_stop();
        break;
    }
    case LV_EVENT_LONG_PRESSED:
    {
        break;
    }
    case LV_EVENT_DRAW_MAIN_BEGIN:
    {
        ;
        break;
    }
    default:
        break;
    }
}

void events_init_wifi (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->wifi_list_wifis_item0, wifi_list_wifis_item0_event_handler, LV_EVENT_ALL, ui);
    ESP_LOGI(TAG, "%s:%d", __func__, __LINE__);
}

static void devices_list_devices_item0_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.settings, guider_ui.settings_del, &guider_ui.devices_del, setup_scr_settings, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 200, false, true);
        break;
    }
    default:
        break;
    }
}

void events_init_devices (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->devices_list_devices_item0, devices_list_devices_item0_event_handler, LV_EVENT_ALL, ui);
}

static void get_passwd_btnm_event_handler(lv_event_t *e, bool isUpper)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_obj_t * obj = lv_event_get_target(e);
        uint32_t id = lv_btnmatrix_get_selected_btn(obj);
        switch (id)
        {
        case (47): // Aa
        {
            lv_obj_clear_flag(guider_ui.get_passwd_btnm_lower, LV_OBJ_FLAG_HIDDEN);
            lv_obj_add_flag(guider_ui.get_passwd_btnm_upper, LV_OBJ_FLAG_HIDDEN);
            break;
        }
        case (48): // OK
        {
            char temp_wifi[64];
            char temp_passwd[64];
            char *wifi = lv_label_get_text(guider_ui.get_passwd_label_wifi);
            char *passwd = lv_label_get_text(guider_ui.get_passwd_label_passwd);
            int auth;
            if(strlen(wifi) > (sizeof(temp_wifi) - 1))
            {
                memcpy(temp_wifi, wifi, sizeof(temp_wifi) - 1);
                temp_wifi[sizeof(temp_wifi) - 1] = '\0';
            }
            else
            {
                memcpy(temp_wifi, wifi, strlen(wifi)+1);
            }
            memcpy(temp_passwd, passwd, strlen(passwd)+1);
            ui_load_scr_animation(&guider_ui, &guider_ui.settings, guider_ui.settings_del, &guider_ui.get_passwd_del, setup_scr_settings, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 200, false, true);
            ESP_LOGI(TAG, "wifi: %s", temp_wifi);
            ESP_LOGI(TAG, "passwd: %s", temp_passwd);
            ESP_LOGI(TAG, "strlen(wifi):%d", strlen(temp_wifi));
            wifi = strstr(temp_wifi, ","); // get channel
            if(wifi == NULL)
            {
                ESP_ERROR_CHECK(ESP_FAIL);
            }
            wifi = strstr(wifi+1, ","); // get rssi
            if(wifi == NULL)
            {
                ESP_ERROR_CHECK(ESP_FAIL);
            }
            wifi = strstr(wifi+1, ","); // get auth
            if(wifi == NULL)
            {
                ESP_ERROR_CHECK(ESP_FAIL);
            }
            wifi[0] = '\0';
            wifi++;
            auth = wifi_sta_auth_method_type(wifi);
            if(auth == -1)
            {
                ESP_ERROR_CHECK(ESP_FAIL);
            }
            wifi = strstr(temp_wifi, ","); // get channel
            wifi[0] = '\0';
            wifi_sta_connect_new_ap(temp_wifi, temp_passwd, auth);
            break;
        }
        case (49): // Cancel
        {
            lv_label_set_text(guider_ui.get_passwd_label_passwd, "");
            ui_load_scr_animation(&guider_ui, &guider_ui.settings, guider_ui.settings_del, &guider_ui.get_passwd_del, setup_scr_settings, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 200, false, true);
            break;
        }
        case (50): // Del
        {
            char *text = lv_label_get_text(guider_ui.get_passwd_label_passwd);
            int num = strlen(text);
            text[num-1] = '\0';
            lv_label_set_text(guider_ui.get_passwd_label_passwd, text);
            break;
        }
        default:
        {
            char tmp_passwd[36];
            char *text = lv_label_get_text(guider_ui.get_passwd_label_passwd);
            int len = strlen(text);
            if(len < (sizeof(tmp_passwd) - 1))
            {
                const char *c = lv_btnmatrix_get_btn_text(obj, id);
                memcpy(tmp_passwd, text, len);
                tmp_passwd[len] = c[0];
                tmp_passwd[len+1] = '\0';
                lv_label_set_text(guider_ui.get_passwd_label_passwd, tmp_passwd);
            }
            break;
        }
        }
        break;
    }
    default:
        break;
    }
}
static void get_passwd_btnm_upper_event_handler (lv_event_t *e)
{
    get_passwd_btnm_event_handler(e, true);
}

static void get_passwd_btnm_lower_event_handler (lv_event_t *e)
{
    get_passwd_btnm_event_handler(e, false);
}

void events_init_get_passwd (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->get_passwd_btnm_upper, get_passwd_btnm_upper_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->get_passwd_btnm_lower, get_passwd_btnm_lower_event_handler, LV_EVENT_ALL, ui);
    ESP_LOGI(TAG, "%s:%d", __func__, __LINE__);
}


void events_init(lv_ui *ui)
{

}
